﻿//-----------------------------------------------------------------------
// <copyright file="CommonPlaceListSettingsPage.xaml.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BookCaching.Settings;
using Microsoft.Phone.Controls;

namespace BookCaching.Pages
{
    public partial class CommonPlaceListSettingsPage : PhoneApplicationPage
    {
        public CommonPlaceListSettingsPage()
        {
            this.InitializeComponent();

            sortOrderListPicker.SelectionChanged += new SelectionChangedEventHandler(SortOrderListPicker_SelectionChanged);
            sortByListPicker.SelectionChanged += new SelectionChangedEventHandler(SortByListPicker_SelectionChanged);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            switch (CurrentSettings.Instance.PlaceListOrderingOrder)
            {
                case OrderingOrder.ascending:
                    this.sortOrderListPicker.SelectedIndex = 0;
                    break;

                case OrderingOrder.descending:
                    this.sortOrderListPicker.SelectedIndex = 1;
                    break;

                default:
                    break;
            }

            switch (CurrentSettings.Instance.PlaceListSortingBy)
            {
                case SortingPlacesBy.distance:
                    this.sortByListPicker.SelectedIndex = 0;
                    break;

                case SortingPlacesBy.name:
                    this.sortByListPicker.SelectedIndex = 1;
                    break;

                default:
                    break;
            }

            if (CurrentSettings.Instance.PlaceIconVisibility == System.Windows.Visibility.Visible)
            {
                this.showIconsCheckBox.IsChecked = true;
            }
            else
            {
                this.showIconsCheckBox.IsChecked = false;
            }
        }

        private void SortOrderListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch ((sender as ListPicker).SelectedIndex)
            {
                case 0:
                    CurrentSettings.Instance.PlaceListOrderingOrder = OrderingOrder.ascending;
                    break;

                case 1:
                    CurrentSettings.Instance.PlaceListOrderingOrder = OrderingOrder.descending;
                    break;

                default:
                    break;
            }
        }

        private void SortByListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch ((sender as ListPicker).SelectedIndex)
            {
                case 0:
                    CurrentSettings.Instance.PlaceListSortingBy = SortingPlacesBy.distance;
                    break;

                case 1:
                    CurrentSettings.Instance.PlaceListSortingBy = SortingPlacesBy.name;
                    break;

                default:
                    break;
            }
        }

        private void showIconsCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CurrentSettings.Instance.PlaceIconVisibility = System.Windows.Visibility.Visible;
        }

        private void showIconsCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            CurrentSettings.Instance.PlaceIconVisibility = System.Windows.Visibility.Collapsed;
        }
    }
}